#include "Hmi/wyzkr688rfswhmi.h"
#include "ui_robotview.h"
#include "Package/QCustomplot/qcustomplot.h"
#include "Package/RobotOpenGLView/RoboticOpenGLView.h"

wyzkr688rfswhmi::wyzkr688rfswhmi(short Local_Remote){
    LocalRemote = Local_Remote;

    this->ui = new Ui::RFSWView;
    this->ui->setupUi(this);

    this->UiDataInit();
    this->RobotScopeInit();
    this->ForceScopeInit();

    this->SignalSlotConnect();
}

wyzkr688rfswhmi::~wyzkr688rfswhmi(){
    this->UiDataRelease();
    this->ForceScopeRelease();
    this->RobotScopeRelease();
}

short wyzkr688rfswhmi::getBackendhandle(BackendInterface* p){
    if(LocalRemote == LOCALCONTOLLER){
        this->pbackendinterface = p;
        return 1;
    }
    else return 0;
}
short  wyzkr688rfswhmi::SignalSlotConnect(){
    return 1;
}

short wyzkr688rfswhmi::UiDataInit(){
    return 1;
}

short wyzkr688rfswhmi::UiDataUpdate(){
    return 1;
}

short wyzkr688rfswhmi::UiDataRelease(){
    return 1;
}

short wyzkr688rfswhmi::ForceScopeInit(){
    ui->ForceScopeView->addGraph();
    ui->ForceScopeView->graph(0)->setPen(QPen(Qt::red));
    ui->ForceScopeView->addGraph();
    ui->ForceScopeView->graph(1)->setPen(QPen(Qt::black));

    ui->ForceScopeView->graph(0)->setAntialiasedFill(true);
    ui->ForceScopeView->yAxis->setRange(-1000,5500);
    ui->ForceScopeView->xAxis->setTickLabelType(QCPAxis::ltDateTime);
    ui->ForceScopeView->xAxis->setDateTimeFormat("h:m:s");
    ui->ForceScopeView->xAxis->setAutoTickStep(false);
    ui->ForceScopeView->xAxis->setTickStep(2);
    ui->ForceScopeView->axisRect()->setupFullAxesBox();
    return NOERROR;
}

short wyzkr688rfswhmi::ForceScopeUpdate(){
    if(this->LocalRemote == LOCALCONTOLLER){
        if(this->pbackendinterface != nullptr){
            this->pbackendinterface->ReadForceControlForceRealValue(mTechData.ForceSensorReal);
            this->pbackendinterface->ReadForceControlForceSetValue(mTechData.ForceProcessSet);
        }
    }
    double key = QDateTime::currentDateTime().toMSecsSinceEpoch()/1000.0;
    static double lastPointKey1 = 0;
    if (key-lastPointKey1 > 0.01){
        ui->ForceScopeView->graph(0)->addData(key, mTechData.ForceSensorReal);
        ui->ForceScopeView->graph(0)->removeDataBefore(key-20);
        ui->ForceScopeView->graph(1)->addData(key, mTechData.ForceProcessSet);
        ui->ForceScopeView->graph(1)->removeDataBefore(key-20);
        lastPointKey1 = key;
    }
    ui->ForceScopeView->xAxis->setRange(key+0.25, 20, Qt::AlignRight);
    ui->ForceScopeView->replot();
    return 1;
}

short wyzkr688rfswhmi::RobotScopeUpdate(){
    double Joint[6];
    if(this->ui->tabWidget->currentIndex() == 0){
        if(this->LocalRemote == LOCALCONTOLLER){
            if(this->pbackendinterface != nullptr){
                pbackendinterface->ReadRobotJointData(mRobot.RobotJointData);
            }
        }
        else{

        }
        Joint[0] = -this->mRobot.RobotJointData[0];
        Joint[1] =  this->mRobot.RobotJointData[1] + 90;
        Joint[2] =  this->mRobot.RobotJointData[2] - 90;
        Joint[3] = -this->mRobot.RobotJointData[3];
        Joint[4] =  this->mRobot.RobotJointData[4];
        Joint[5] = -this->mRobot.RobotJointData[5];
        this->ui->RobotView->RoboticOpenViewUpdate(Joint);
    }
    return 1;
}

void wyzkr688rfswhmi::RemoteDataRecv(QByteArray Data){
    double tempRecvValue[6];
    if(this->LocalRemote == LOCALCONTOLLER){
    }
    else{
        int loopi = 0;
        QXmlStreamReader mQXmlStreamReader;
        mQXmlStreamReader.addData(Data);
        while(!mQXmlStreamReader.atEnd()){
            if(mQXmlStreamReader.isStartElement()){
                if(mQXmlStreamReader.name() == "AIst"){
                    loopi = 0;
                    foreach (const QXmlStreamAttribute & attribute, mQXmlStreamReader.attributes()){
                        QStringRef tempStringRef;
                        QString  tempQString;
                        tempStringRef = attribute.value();
                        tempQString = tempStringRef.toString();
                        mRobot.RobotJointData[loopi] = tempQString.toDouble();
                        loopi++;
                    }
                }
                else if(mQXmlStreamReader.name() == "ATIst"){
                    loopi = 0;
                    foreach (const QXmlStreamAttribute & attribute, mQXmlStreamReader.attributes()){
                        QStringRef tempStringRef;
                        QString  tempQString;
                        tempStringRef = attribute.value();
                        tempQString = tempStringRef.toString();
                        mRobot.RobotJointTorData[loopi]= tempQString.toDouble();
                        loopi++;
                    }
                }
                else if(mQXmlStreamReader.name() == "TechInfo"){
                    loopi = 0;
                    foreach (const QXmlStreamAttribute & attribute, mQXmlStreamReader.attributes()){
                        QStringRef tempStringRef;
                        QString  tempQString;
                        tempStringRef = attribute.value();
                        tempQString = tempStringRef.toString();
                        tempRecvValue[loopi] = tempQString.toDouble();
                        loopi++;
                    }
                }
            }
            mQXmlStreamReader.readNext();
        }
        mTechData.SpindleSpeedReal = tempRecvValue[0];
        mTechData.ForceProcessSet = tempRecvValue[1];
        mTechData.ForceSensorReal = tempRecvValue[2];
        mQXmlStreamReader.clear();
    }
}

void wyzkr688rfswhmi::RemoteDataSend(QByteArray& Data){
    if(this->LocalRemote == LOCALCONTOLLER){
        char tempSend[64];
        QString SendString;
        QXmlStreamWriter stream(&SendString);

        memset(tempSend,'\0',strlen(tempSend));
        stream.writeStartElement("TecController");
            stream.writeAttribute("Tec","UnionTec");

            stream.writeEmptyElement("AIst");
                sprintf(tempSend,"%f", mRobot.RobotJointData[0]);
                    stream.writeAttribute("A1",tempSend);
                sprintf(tempSend,"%f", mRobot.RobotJointData[1]);
                    stream.writeAttribute("A2",tempSend);
                sprintf(tempSend,"%f", mRobot.RobotJointData[2]);
                    stream.writeAttribute("A3",tempSend);
                sprintf(tempSend,"%f", mRobot.RobotJointData[3]);
                    stream.writeAttribute("A4",tempSend);
                sprintf(tempSend,"%f", mRobot.RobotJointData[4]);
                    stream.writeAttribute("A5",tempSend);
                sprintf(tempSend,"%f", mRobot.RobotJointData[5]);
                    stream.writeAttribute("A6",tempSend);

            stream.writeEmptyElement("ATIst");
                sprintf(tempSend,"%f", mRobot.RobotJointTorData[0]);
                    stream.writeAttribute("A1T",tempSend);
                sprintf(tempSend,"%f", mRobot.RobotJointTorData[1]);
                    stream.writeAttribute("A2T",tempSend);
                sprintf(tempSend,"%f", mRobot.RobotJointTorData[2]);
                    stream.writeAttribute("A3T",tempSend);
                sprintf(tempSend,"%f", mRobot.RobotJointTorData[3]);
                    stream.writeAttribute("A4T",tempSend);
                sprintf(tempSend,"%f", mRobot.RobotJointTorData[4]);
                    stream.writeAttribute("A5T",tempSend);
                sprintf(tempSend,"%f", mRobot.RobotJointTorData[5]);
                    stream.writeAttribute("A6T",tempSend);
            stream.writeEndElement();

            stream.writeEmptyElement("TechInfo");
                sprintf(tempSend,"%d", mTechData.SpindleSpeedReal);
                    stream.writeAttribute("Spindle",tempSend);
                sprintf(tempSend,"%f", mTechData.ForceProcessSet);
                    stream.writeAttribute("FzSet",tempSend);
                sprintf(tempSend,"%f", mTechData.ForceSensorReal);
                    stream.writeAttribute("FzReal",tempSend);
            stream.writeEndElement();
        stream.writeEndElement();
        Data = SendString.toLocal8Bit();
    }
    else{ }
}

short wyzkr688rfswhmi::ForceScopeRelease(){return 1;}
short wyzkr688rfswhmi::RobotScopeInit(){return 1;}
short wyzkr688rfswhmi::RobotScopeRelease(){return 1;}
